package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.dao.CommentMapper;
import com.qf.dao.LikeDao;
import com.qf.dao.LoveshowMapper;
import com.qf.dao.UserMapper;
import com.qf.dto.ShowDto;
import com.qf.entity.Comment;
import com.qf.entity.Loveshow;
import com.qf.entity.User;
import com.qf.enums.PageEnum;
import com.qf.service.CommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chen
 * @version 1.0
 * @date 2019/10/24 8:58
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Resource
    private LoveshowMapper loveshowMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private LikeDao likeDao;

    @Resource
    private CommentMapper commentMapper;

    @Override
    public List getCommunitys(Integer pageIndex) {

        PageHelper.startPage(pageIndex, PageEnum.TEN.getPageIndex());
        List<Loveshow> loveshows = loveshowMapper.getCommunitys();
        PageInfo<Loveshow> pageInfo = new PageInfo<>(loveshows);

        List list = new ArrayList();

        for (Loveshow loveshow : pageInfo.getList()) {
            ShowDto showDto = new ShowDto();
            BeanUtils.copyProperties(loveshow, showDto);
            User u = userMapper.getLoveUser(showDto.getUserid());
            showDto.set_user(u.getKickname());
            showDto.set_userid(u.getUserid());
            int likeCount = likeDao.getLikeCount(showDto.getShowid());
            showDto.setLikecount(likeCount);
            int commentCount = commentMapper.getCommentCount(showDto.getShowid());
            showDto.setCommentCount(commentCount);
            list.add(showDto);
        }

        return list;
    }

    @Override
    public List<Comment> getComments(Integer showId) {
        List<Comment> commentList = commentMapper.getComments(showId);
        return commentList;
    }

    @Override
    public int addComment(String commentText, Integer showId, int userid) {
        Map map = new HashMap();
        map.put("commentText", commentText);
        map.put("showId", showId);
        map.put("userid", userid);
        int i = commentMapper.addComment(map);
        return i;
    }
}
